package com.ssb.admin.modules.sys.user.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ssb.admin.common.controller.AbstractController;
import com.ssb.admin.common.utils.R;
import com.ssb.admin.common.utils.ShiroUtils;
import com.ssb.admin.common.utils.UploadUtils;
import com.ssb.admin.constant.Constants;
import com.ssb.admin.modules.sys.role.entity.SysRole;
import com.ssb.admin.modules.sys.role.entity.SysUserRole;
import com.ssb.admin.modules.sys.role.service.ISysRoleService;
import com.ssb.admin.modules.sys.role.service.ISysUserRoleService;
import com.ssb.admin.modules.sys.user.controller.dto.UserQuery;
import com.ssb.admin.modules.sys.user.entity.SysUser;
import com.ssb.admin.modules.sys.user.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author duaichao
 * @since 2018-08-23
 */
@Api(value = "用户管理", tags = "用户管理")
@RestController
public class SysUserController extends AbstractController {
        private final ISysUserService sysUserService;
        private final ISysUserRoleService sysUserRoleService;
        private final ISysRoleService sysRoleService;
        @Autowired
        public SysUserController(ISysUserService sysUserService,
                               ISysUserRoleService sysUserRoleService,
                               ISysRoleService sysRoleService
        ) {
            this.sysUserService = sysUserService;
            this.sysUserRoleService = sysUserRoleService;
            this.sysRoleService = sysRoleService;
        }



        @GetMapping("/user/info")
        @ApiOperation(value = "获取用户信息", notes = "用于登录成功后获取用户信息")
        @ApiResponse(code = 50014, message = "Token已失效，自动跳转登录...")
        public R getUserInfo(HttpServletRequest request){
            if(ShiroUtils.getUserInfo()!=null){
                return R.ok().data(ShiroUtils.getUserInfo());
            }
            Long userId = (Long)request.getAttribute(Constants.USER_ID);
            SysUser sysUser;
            // 获取用户
            Wrapper<SysUser> ew = new EntityWrapper<SysUser>()
                    .eq("id", userId);
            sysUser = sysUserService.selectOne(ew);
            Wrapper<SysUserRole> ewr = new EntityWrapper<SysUserRole>().eq("user_id", sysUser.getId());
            SysUserRole sysUserRoleEntity = sysUserRoleService.selectOne(ewr);
            SysRole sysRole = sysRoleService.selectById(sysUserRoleEntity.getRoleId());
            sysUser.setRole(sysRole);
            return R.ok().data(sysUser);
        }

        @PostMapping("/user/list")
        @ApiOperation(value = "获取用户列表", notes = "用于用户管理列表")
        public R getUserList(@RequestBody UserQuery dto){
            return sysUserService.queryPage(dto);
        }

        @PostMapping("/user/create")
        @ApiImplicitParam(name="sysUser", value="用户对象实例", required = true, dataType = "SysUser")
        @ApiOperation(value = "创建用户", notes = "创建用户")
        public R createUser(@RequestBody SysUser sysUser, @Value("${ssadmin.default.password}") String defaultPwd){
            // 设置配置文件的默认密码
            sysUser.setPassword(defaultPwd);
            Long id = sysUserService.save(sysUser);
            return R.ok().data("id", id);
        }
        @PostMapping("/user/update")
        @ApiImplicitParam(name="sysUser", value="用户对象实例", required = true, dataType = "SysUser")
        @ApiOperation(value = "更新用户", notes = "更新用户")
        public R updateUser(@RequestBody SysUser sysUser){
            sysUserService.save(sysUser);
            return R.ok();
        }
        @PostMapping("/user/delete")
        @ApiImplicitParam(name="sysUser", value="用户对象实例", required = true, dataType = "SysUser")
        @ApiOperation(value = "删除用户", notes = "删除用户")
        public R deleteUser(@RequestBody SysUser sysUser){
            sysUserService.del(sysUser);
            return R.ok();
        }
        @PostMapping("/user/avatar")
        @ApiImplicitParam(name="avatar", value="用户头像图片", required = true, dataType = "file")
        @ApiOperation(value = "上传用户头像", notes = "上传用户头像")
        public R uploadAvatar(@RequestParam("avatar") MultipartFile avatar, @RequestParam("id") Long id){
            if (avatar.isEmpty()){
                return R.error("上传文件为空");
            }
            String image = "data:img/png;base64,"+UploadUtils.uploadBase64File(avatar);
            SysUser sysUser = new SysUser();
            sysUser.setId(id);
            sysUser.setAvatar(image);
            sysUserService.save(sysUser);
            return R.ok().data("avatar", image);
        }

}

